package com.yongcheon.sms_manager.ui.phone_spam.list;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.data.phone_spsm.PhoneData;
import com.yongcheon.sms_manager.data.phone_spsm.SpamPhonData;
import com.yongcheon.sms_manager.data.preference.SpamNumberData;
import com.yongcheon.sms_manager.database.SpamDBManager;
import com.yongcheon.sms_manager.vo.ContactVO;
import com.yongcheon.sms_manager.vo.PhoneVO;

import java.util.ArrayList;

/**
 * Created by yongcheon on 2014-11-27.
 */
public class PhoneSPAMLongClickListener implements View.OnLongClickListener{

    PhoneVO vo;
    ContactVO contactvo;
    PhoneSPAMCurosrAdapter cursorAdapter;

    public PhoneSPAMLongClickListener(PhoneVO vo, ContactVO contactvo, PhoneSPAMCurosrAdapter cursorAdapter){
        super();
        this.vo = vo;
        this.contactvo = contactvo;
        this.cursorAdapter = cursorAdapter;
    }

    @Override
    public boolean onLongClick(View v) {
        String[] MENU_LIST = {v.getContext().getResources().getString(R.string.delete_this_call_log), v.getContext().getResources().getString(R.string.delete_same_number),
                              v.getContext().getResources().getString(R.string.unregister_spam)};
        String from = (contactvo != null) ? contactvo.getName() : vo.getNumber();
        AlertDialog dialog = new AlertDialog.Builder(v.getContext())
                .setTitle(from)
                .setItems(MENU_LIST, new DialogOnClickListener(v.getContext()))
                .create();
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        return false;
    }

    class DialogOnClickListener implements  DialogInterface.OnClickListener{
        private Context context;

        public DialogOnClickListener(Context context){
            super();
            this.context = context;
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            PhoneData phoneData = new PhoneData();
            SpamPhonData spamData = new SpamPhonData();
            SpamNumberData spamNumData = new SpamNumberData(context);

            SQLiteDatabase database = SpamDBManager.getDatabase(context);
            switch (which){
                case 0 :
                    spamData.delete(PhoneSPAMLongClickListener.this.vo.getId(), context, database);
                    cursorAdapter.getCursor().requery();
                    break;
                case 1 :
                    spamData.deleteByPhoneNum(PhoneSPAMLongClickListener.this.vo.getNumber(), context, database);
                    cursorAdapter.getCursor().requery();
                break;
                case 2 :
                    unRegistSpam(database, spamData, phoneData, spamNumData);
                    break;
            }
        }

        public void unRegistSpam(SQLiteDatabase database, SpamPhonData spamData, PhoneData phoneData, SpamNumberData spamNumData){
            try {
                database.beginTransaction();

                ArrayList<PhoneVO> aList = spamData.select(PhoneSPAMLongClickListener.this.vo.getNumber(), context, database);

                for (int i = 0; i < aList.size(); i++) {
                    spamData.delete(aList.get(i).getId(), context, database);
                }

                for (int i = 0; i < aList.size(); i++) {
                    phoneData.insert(aList.get(i), context, null);
                }

                spamNumData.delete(PhoneSPAMLongClickListener.this.vo.getNumber(), context, database);

                cursorAdapter.getCursor().requery();
                database.setTransactionSuccessful();
            }catch(Exception e){
                e.printStackTrace();
            }finally {
                database.endTransaction();
            }
        }
    }
}
